package com.william.lifetraxer.util;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;


@XmlRootElement(name="points")
public class Track {
	
	@XmlAttribute
	public String date = null;
	
	@XmlAttribute
	public String username = null;
	
	@XmlAttribute
	public int groupid = 0;
	
	@XmlAttribute
	public int type = 0;
	
	@XmlAttribute
	public String name = null;
	
	@XmlElement(name="point")
	List<Point> points = new ArrayList<Point>();
	
	public Track()
	{
		this(null,null,0,null,0,null);
	}
	
	public Track(List<Point> points){
		this.points=points;
	}
	
	public Track(String date , String username , int groupid , String name , int type , List<Point> points)
	{
		this.date=date;
		this.username=username;
		this.groupid=groupid;
		this.points=points;
		this.type=type;
		this.name=name;
	}
	
	public int length(){
		return this.points.size();
	}
	
	public Point[] toPointArray(){
		return this.points.toArray(new Point[]{});
	}
	
	public void set(int index , Point point){
		this.points.set(index, point);
	}
	
	public Point get(int index){
		return this.points.get(index);
	}
	
	public void appendTrack(Track appendTrack){
		this.points.addAll(appendTrack.points);
	}
	
	public static void main(String[] args)
	{
		try{
			JAXBContext context = JAXBContext.newInstance(Track.class);
			Marshaller m = context.createMarshaller();
			
			Date now = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String date = sdf.format(now);
			
			List<Point> ps = new ArrayList<Point>();
			ps.add(new Point(92.0,101.0));
			ps.add(new Point(93.0,102.0));
			ps.add(new Point(95.0,104.0));
			ps.add(new Point(96.0,107.0));
			ps.add(new Point(96.0,108.0));
			ps.add(new Point(98.0,110.0));
			ps.add(new Point(99.0,111.0));
			
			Track t = new Track(date,"william",3,"test",11,ps);
			
			File file = new File("G:\\point5.xml");
			
			m.marshal(t, file);
			
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

}
